<?php
namespace app\atxunda\controller;
use think\Controller;
use think\Db; 
use think\Request;
use think\Cache;

class nav extends Common
{
	/**
	 * [nav 课程导航显示页面]
	 * @return [type] [description]
	 */
	public function index(){
		if(input('post.')){
			$nav_ids = input('post.nav_id/a');
			$nav_sorts = input('post.nav_sort/a');
			$status = input('post.status');
			if($status == 'del'){
				$nav_id = implode(',', $nav_ids);
				$del = Db::table('nav')->delete([$nav_id]);
				if($del){
					$this->success('删除成功','index','',1);
				}
			}else{
				foreach ($nav_ids as $key => $id) {
					$up = Db::table('nav')->where('nav_id',$id)->update(['nav_sort'=>$nav_sorts[$id]]);
				}
				$this->success('排序成功','index','',1);
			}

		}else{
			$data = Db::table('nav')->order('nav_sort desc')->select();
			$this->assign('data',$data);
			return $this->fetch('nav');
		}
	}


	/**
	 * [navedit 课程导航修改页面]
	 * @return [type] [description]
	 */
	public function navedit(){
		$request = Request::instance();
		$nav_id = $request->param('nav_id');
		$nav_data = Db::table('nav')->where('nav_id',$nav_id)->find();
		if(input('post.')){
			$req = input('post.');
			$pack_id = input('post.pack_id/a');
			$price = input('post.price/a');
			$original_price = input('post.original_price/a');
			unset($req['price']);
			unset($req['pack_id']);
			if ($nav_id){
				$variable = Db::table('nav')->where('nav_id',$nav_id)->update($req);;
			}else{
				$req['nav_price'] = $price[1] ?: $price[0];
				$req['original_price'] = $original_price[1] ?: $original_price[0];
				$variable = Db::table('nav')->insert($req);
				$nav_id = Db::table('nav')->getLastInsID();
			}
			Db::table('nav_package')->where('nav_id',$nav_id)->delete();
			$arr = array();
			foreach ($pack_id as $key => $id) {
				if($price[$key]){
					$arr[$key]['nav_id'] = $nav_id;
					$arr[$key]['pack_id'] = $id;
					$arr[$key]['price'] = $price[$key];
					$arr[$key]['original_price'] = $original_price[$key];
					$arr[$key]['time'] = date('Y-m-d H:i:s');
				}
			}
			$model = new  \app\index\model\NavPackage;
			$model->saveAll($arr);
			$this->success('编辑成功','index','',1);
		}else{
			$pack_data = Db::table('package')->select();
			foreach ($pack_data as $key => &$v) {
				$v['pn'] = Db::table('nav_package')->where(['pack_id'=>$v['id'],'nav_id'=>$nav_id])->find();
			}
			$this->assign('nav_data',$nav_data);
			$this->assign('pack_data',$pack_data);
			return $this->fetch('navedit');
		}
	}


	/**
	 * [navdelete 课程导航删除]
	 * @return [type] [description]
	 */
	public function navdelete(){
		$request = Request::instance();
		$nav_id = $request->param('nav_id');
		if(Db::table('nav')->where('nav_id',$nav_id)->delete()){
			Db::table('type')->where('nav_id',$nav_id)->delete();
			Db::table('course')->where('nav_id',$nav_id)->delete();
			Db::table('specific')->where('nav_id',$nav_id)->delete();
			Db::table('package_nav')->where('nav_id',$nav_id)->delete();
			$this->success('删除成功','index','',1);
		}else{
			$this->error('删除失败');
		}
	}

	public function hits(){
		$request = Request::instance();
		$nav_id = $request->param('nav_id');
		$data = Db::table('nav_hits')->where('navId',$nav_id)->paginate(30);
		$monthHits = Db::table('nav_hits')->where(['navId'=>$nav_id,'time'=>['like',date('Y-m').'%']])->select();
		$themeMonth = array_column($monthHits,'theme');
		$syllabusMonth = array_column($monthHits,'syllabus');
		$mapMonth = array_column($monthHits,'map');
		$weekHits = Db::table('nav_hits')->where(['navId'=>$nav_id,'time'=>['like',date('Y').'%'],'WEEK(`time`)'=>date('W')-1])->select();
		$themeWeek = array_column($weekHits,'theme');
		$syllabusWeek = array_column($weekHits,'syllabus');
		$mapWeek = array_column($weekHits,'map');
		return $this->fetch('hits',[
			'themeMonth' => array_sum($themeMonth),
			'syllabusMonth' => array_sum($syllabusMonth),
			'mapMonth' => array_sum($mapMonth),
			'themeWeek' => array_sum($themeWeek),
			'syllabusWeek' => array_sum($syllabusWeek),
			'mapWeek' => array_sum($mapWeek),
			'data' => $data,
		]);
	}
}
?>